package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/permission/")
public class PermissionController {
	@Reference
	private PermissionService permissionService;


	@RequestMapping("findAll")
	public Result findAll(){
		try {
			List<Permission> permissions = permissionService.findAll();
			return new Result(true, "查询所有权限成功", permissions);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"查询所有权限失败");
		}
	}

	//通过条件分页查询权限
	@RequestMapping("findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
		return permissionService.findPage(queryPageBean);
	}

	//直接添加权限
	@RequestMapping("add")
	public Result addPermission(@RequestBody Permission permission) {
		try {
			System.out.println(permission);
			permissionService.addPermission(permission);
			return new Result(true, "添加权限成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "添加权限失败，请联系管理员");
		}
	}

	//删除权限
	@RequestMapping("delete")
	public Result deleteById(Integer id) {
		try {
			System.out.println(id);
			permissionService.deleteById(id);
			return new Result(true, "删除成功");
		} catch (RuntimeException e) {
			return new Result(false, e.getMessage());
		} catch (Exception e) {
			return new Result(false, "删除过程出现一些问题，请联系管理员");
		}
	}
	//根据ID查询权限
	@RequestMapping("findById")
	public Result findById(Integer id){
		return new Result(true,"获得权限信息成功",permissionService.findById(id));
	}

	//修改权限
	@RequestMapping("edit")
	public Result editPermission(@RequestBody Permission permission){
		try {
			permissionService.edit(permission);
			return new Result(true, "修改权限信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true, "修改失败");
		}
	}
}
